﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.ModelConfiguration.Conventions;
using Liang.DataBase.Model.Entity;
using System.Configuration;
using System.Reflection;
using System.Data.Entity.ModelConfiguration;

using Liang.DataBase.Mapping.MySql;

namespace Liang.DataBase.DAL
{
    [DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
    public class DBEntities : DbContext
    {
        /// <summary>
        /// 动态构造函数
        /// </summary>
        public DBEntities() : base(nameOrConnectionString: ConnectionString())
        {
        }

        /// <summary>
        /// 获取连接数据库字符串的名称
        /// </summary>
        /// <returns></returns>
        private static string ConnectionString()
        {
            string dbType = ConfigurationManager.AppSettings["ComponentDbType"];
            if (string.IsNullOrEmpty(dbType))
            {
                dbType = "sqlserver";
            }
            return string.Format("name={0}", dbType.ToLower());
        }

        public DbSet<sys_app> apps { get; set; }
        public DbSet<sys_basicdata> basicdatas { get; set; }
        public DbSet<sys_config> configs { get; set; }
        public DbSet<sys_module> modules { get; set; }
        public DbSet<sys_organ> oragns { get; set; }
        public DbSet<sys_role> roles { get; set; }
        public DbSet<sys_user> users { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            //modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();//移除复数表名的契约
            //modelBuilder.Conventions.Remove<IncludeMetadataConvention>();//防止黑幕交易 要不然每次都要访问 

            // 关闭一对多级联
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
            // 关闭多对多级联
            modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();

            modelBuilder.Configurations.Add(new AppMap());
            modelBuilder.Configurations.Add(new BasicdataMap());
            modelBuilder.Configurations.Add(new ConfigMap());
            modelBuilder.Configurations.Add(new ModuleMap());
            modelBuilder.Configurations.Add(new OrganMap());
            modelBuilder.Configurations.Add(new RoleMap());
            modelBuilder.Configurations.Add(new UserMap());

            //string mapType = ConfigurationManager.AppSettings["ComponentDbType"];
            //var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
            //    .Where(type => type.Namespace.EndsWith(mapType, StringComparison.OrdinalIgnoreCase))
            //    .Where(type => !String.IsNullOrEmpty(type.Namespace))
            //    .Where(type => type.BaseType != null && type.BaseType.IsGenericType
            //        && type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));

            //foreach (var type in typesToRegister)
            //{
            //    dynamic configurationInstance = Activator.CreateInstance(type);
            //    modelBuilder.Configurations.Add(configurationInstance);
            //}
        }
    }
}
